﻿$(document).ready(function () {
    function BindTips() {
        //Invoke AJAX method to get all the Tips
        $.post(pathPrefix + "Tips/GetTipsList", null, function (result) {
            $("#tipTemplate").tmpl(result).appendTo("#tblTips");

            tableAltRows();
        }, "json");
    }

    BindTips();

    //when the user changes one of the input elements in a row
    $("tr :input").live("change", function () {
        var row = $(this).parents("tr");

        //get all the tip data
        var data = row.find(":input").serializeObject();

        if (data.TipText != "") {
            //Invoke AJAX method to update this Approval Check (insert or edit)
            $.post(pathPrefix + "Tips/Update", data, function (result) {
                //update the TipId value (if inserted row)
                row.find("#TipId").val(result);
                row.find(".lnkDeleteTip").show();

                row.find(".successCell img").fadeIn(1000).delay(2000).fadeOut();
            }, "json");
        }
    });

    $(".lnkDeleteTip").live("click", function () {
        var row = $(this).parents("tr");
        var id = row.find("[name='TipId']").val();

        openConfirmDialog("האם אתה בטוח?", function () {
            //Invoke AJAX method to delete the Tip
            $.post(pathPrefix + "Tips/Delete", { "id": id }, function (result) {
                row.remove();
            }, "json");
        });

        return false;
    });

    //when the user clicks "AddTip"
    $("#btnAddTip").click(function () {
        //add new row using the template
        var row = $("#tipTemplate").tmpl(null).appendTo("#tblTips");

        row.find(":input").val("");

        row.find(".lnkDeleteTip").hide();

        tableAltRows();
    });
});